/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model.pojo.util;

import java.util.List;
import javax.persistence.*;
import model.pojo.*;

/**
 *
 * @author Yasith
 */
public class helper {
    
    static EntityManagerFactory emf;
    
    static {
        emf = Persistence.createEntityManagerFactory("fanboxPU");
    }
    
    public static EntityManagerFactory getEMF(){
        return emf;
    }

public static void help(){
    
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("fanboxPU");
        // First unit of work
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        //Message message = new Message("Hello World");
        User u = new User();
        u.setDisplayName("user 1");
        u.setEmail("user1@gmaila.com");
        u.setPwd("secret");
        UserProfile up = new UserProfile();
        up.setAboutMe("this is something about me");
        up.setAge(32);
        up.setGender("male");
        up.setUserID(u.getUserID());
        em.persist(u);
        tx.commit();
        em.close();
        // Second unit of work
        EntityManager newEm = emf.createEntityManager();
        EntityTransaction newTx = newEm.getTransaction();
        newTx.begin();
        List messages = newEm
            .createQuery("select m from User m")
            .getResultList();
    
        System.out.println( messages.size() + " message(s) found" );
        for (Object m : messages) {
            User loadedMsg = (User) m;
            System.out.println(loadedMsg.getDisplayName());
        }
        newTx.commit();
        newEm.close();
        // Shutting down the application
        emf.close();

}

}
